/*
 * init_cg.c
 *
 *  Created on: 2012/09/04
 *      Author: shenming
 */

#include <stdio.h>
#include "sqlite3.h"
#include "typedef.h"
#include "db_def.h"
#include "core.h"
#include "Id.h"

/*TBL_MEMBER*/
static INT createMember(sqlite3 *db) {

	INT rc = 0;
	PSZ zErrMsg = NULL;
	PSZ sql_create = NULL;

	sql_create = sqlite3_mprintf("CREATE TABLE %q (TblIndex INTEGER,"
			"%q INTEGER, %q INTEGER, %q TEXT, %q INTEGER,"
			"%q INTEGER, %q INTEGER, %q INTEGER,PRIMARY KEY(TblIndex));",
			TBL_MEMBER, _T_IDX(2), _T_IDX(3 ), _T_IDX(4 ), _T_IDX(5 ),
			_T_IDX(6), _T_IDX(7), _T_IDX(8));
	rc = sqlite3_exec(db, sql_create, 0, 0, &zErrMsg);
	if (rc != SQLITE_OK) {
		return rc;
	} else {
		if (sql_create != NULL ) {
			sqlite3_free(sql_create);
		}
	}
	return rc;
}

/*TBL_CONSTANT*/
static INT createConstant(sqlite3 *db) {

	INT rc = 0;
	PSZ zErrMsg = NULL;
	PSZ sql_create = NULL;

	sql_create = sqlite3_mprintf("CREATE TABLE %q (TblIndex INTEGER,"
			"%q INTEGER, %q INTEGER, %q INTEGER, %q INTEGER,"
			"%q INTEGER, %q DOUBLE,PRIMARY KEY(TblIndex));", TBL_CONSTANT,
			_T_IDX(2), _T_IDX(3 ), _T_IDX(4 ), _T_IDX(5 ), _T_IDX(6),
			_T_IDX(7));
	rc = sqlite3_exec(db, sql_create, 0, 0, &zErrMsg);
	if (rc != SQLITE_OK) {
		return rc;
	} else {
		if (sql_create != NULL ) {
			sqlite3_free(sql_create);
		}
	}
	return rc;
}

/*TBL_GBLLINKAGE*/
static INT createGblLinkage(sqlite3 *db) {

	INT rc = 0;
	PSZ zErrMsg = NULL;
	PSZ sql_create = NULL;

	sql_create =
			sqlite3_mprintf(
					"CREATE TABLE %q (TblIndex INTEGER,"
							"%q INTEGER, %q INTEGER, %q INTEGER, %q INTEGER,PRIMARY KEY(TblIndex));",
					TBL_GBLLINKAGE, _T_IDX(2), _T_IDX(3 ), _T_IDX(4 ),
					_T_IDX(5 ));
	rc = sqlite3_exec(db, sql_create, 0, 0, &zErrMsg);
	if (rc != SQLITE_OK) {
		return rc;
	} else {
		if (sql_create != NULL ) {
			sqlite3_free(sql_create);
		}
	}
	return rc;
}

/*TBL_FUNCTIONIDINFO*/
static INT createFunctionIdInfo(sqlite3 *db) {

	INT rc = 0;
	PSZ zErrMsg = NULL;
	PSZ sql_create = NULL;

	sql_create =
			sqlite3_mprintf(
					"CREATE TABLE %q (TblIndex INTEGER,"
							"%q INTEGER, %q INTEGER, %q INTEGER, %q TEXT, %q INTEGER ,%q INTEGER, %q INTEGER,PRIMARY KEY(TblIndex));",
					TBL_FUNCTIONIDINFO, _T_IDX(2), _T_IDX(3), _T_IDX(4),
					_T_IDX(5), _T_IDX(6), _T_IDX(7), _T_IDX(8));
	rc = sqlite3_exec(db, sql_create, 0, 0, &zErrMsg);
	if (rc != SQLITE_OK) {
		return rc;
	} else {
		if (sql_create != NULL ) {
			sqlite3_free(sql_create);
		}
	}
	return rc;
}

/*TBL_VARIABLEINFO*/
static INT createVariableInfo(sqlite3 *db) {

	INT rc = 0;
	PSZ zErrMsg = NULL;
	PSZ sql_create = NULL;

	sql_create =
			sqlite3_mprintf(
					"CREATE TABLE %q (TblIndex INTEGER,"
							"%q INTEGER, %q INTEGER, %q INTEGER, %q TEXT, %q INTEGER, %q INTEGER, %q INTEGER,"
							"%q INTEGER, %q INTEGER, %q INTEGER, %q INTEGER, %q INTEGER, %q INTEGER, %q INTEGER,"
							"%q INTEGER, %q INTEGER, PRIMARY KEY(TblIndex));",
					TBL_VARIABLEINFO, _T_IDX(2), _T_IDX(3), _T_IDX(4),
					_T_IDX(5), _T_IDX(6), _T_IDX(7), _T_IDX(8), _T_IDX(9),
					_T_IDX(10), _T_IDX(11), _T_IDX(12), _T_IDX(13), _T_IDX(14),
					_T_IDX(15), _T_IDX(16), _T_IDX(17));
	rc = sqlite3_exec(db, sql_create, 0, 0, &zErrMsg);
	if (rc != SQLITE_OK) {
		return rc;
	} else {
		if (sql_create != NULL ) {
			sqlite3_free(sql_create);
		}
	}
	return rc;
}

/*TBL_COMMANDLIST*/
static INT createCmdList(sqlite3 *db) {

	INT rc = 0;
	PSZ zErrMsg = NULL;
	PSZ sql_create = NULL;

	sql_create =
			sqlite3_mprintf(
					"CREATE TABLE %q (TblIndex INTEGER,"
							"%q INTEGER, %q INTEGER, %q INTEGER, %q INTEGER, %q INTEGER,"
							"%q INTEGER, %q INTEGER, %q INTEGER, %q INTEGER, %q TEXT,PRIMARY KEY(TblIndex));",
					TBL_COMMANDLIST, _T_IDX(2), _T_IDX(3 ), _T_IDX(4 ),
					_T_IDX(5 ), _T_IDX(6), _T_IDX(7), _T_IDX(8), _T_IDX(9),
					_T_IDX(10), _T_IDX(11));
	rc = sqlite3_exec(db, sql_create, 0, 0, &zErrMsg);
	if (rc != SQLITE_OK) {
		return rc;
	} else {
		if (sql_create != NULL ) {
			sqlite3_free(sql_create);
		}
	}
	return rc;
}

/*TBL_OPERAND*/
static INT createOperand(sqlite3 *db) {

	INT rc = 0;
	PSZ zErrMsg = NULL;
	PSZ sql_create = NULL;

	sql_create =
			sqlite3_mprintf(
					"CREATE TABLE %q (TblIndex INTEGER,%q INTEGER, %q INTEGER,PRIMARY KEY(TblIndex));",
					TBL_OPERAND, _T_IDX(2), _T_IDX(3));
	rc = sqlite3_exec(db, sql_create, 0, 0, &zErrMsg);
	if (rc != SQLITE_OK) {
		return rc;
	} else {
		if (sql_create != NULL ) {
			sqlite3_free(sql_create);
		}
	}
	return rc;
}

/*TBL_LINKTYPEINFO*/
static INT createLinkType(sqlite3 *db) {

	INT rc = 0;
	PSZ zErrMsg = NULL;
	PSZ sql_create = NULL;

	sql_create =
			sqlite3_mprintf(
					"CREATE TABLE %q (TblIndex INTEGER,"
							"%q INTEGER, %q INTEGER, %q INTEGER, %q INTEGER, %q INTEGER,"
							"%q INTEGER, %q INTEGER, %q INTEGER, %q INTEGER, %q INTEGER,"
							"%q INTEGER, %q TEXT, %q INTEGER, %q INTEGER,PRIMARY KEY(TblIndex));",
					TBL_LINKTYPEINFO, _T_IDX(2), _T_IDX(3), _T_IDX(4),
					_T_IDX(5), _T_IDX(6), _T_IDX(7), _T_IDX(8), _T_IDX(9),
					_T_IDX(10), _T_IDX(11), _T_IDX(12), _T_IDX(13), _T_IDX(14),
					_T_IDX(15));
	rc = sqlite3_exec(db, sql_create, 0, 0, &zErrMsg);
	if (rc != SQLITE_OK) {
		return rc;
	} else {
		if (sql_create != NULL ) {
			sqlite3_free(sql_create);
		}
	}
	return rc;
}

/*TBL_TRANSUNIT*/
static INT createTransUnit(sqlite3 *db) {

	INT rc = 0;
	PSZ zErrMsg = NULL;
	PSZ sql_create = NULL;

	sql_create =
			sqlite3_mprintf(
					"CREATE TABLE %q (TblIndex INTEGER,"
							"%q INTEGER, %q INTEGER, %q INTEGER, %q INTEGER, %q INTEGER, PRIMARY KEY(TblIndex));",
					TBL_TRANSUNIT, _T_IDX(2), _T_IDX(3), _T_IDX(4), _T_IDX(5),
					_T_IDX(6));
	rc = sqlite3_exec(db, sql_create, 0, 0, &zErrMsg);
	if (rc != SQLITE_OK) {
		return rc;
	} else {
		if (sql_create != NULL ) {
			sqlite3_free(sql_create);
		}
	}
	return rc;
}

/*initialize cg db*/
INT DLL_EXPORT initCgDB(sqlite3 * db) {
	INT rc = 0;

	rc = createTransUnit(db);
	if (rc != COEXIT_SUCCESS) {
		return rc;
	}

	rc = createMember(db);
	if (rc != COEXIT_SUCCESS) {
		return rc;
	}

	rc = createConstant(db);
	if (rc != COEXIT_SUCCESS) {
		return rc;
	}

	rc = createGblLinkage(db);
	if (rc != COEXIT_SUCCESS) {
		return rc;
	}

	rc = createFunctionIdInfo(db);
	if (rc != COEXIT_SUCCESS) {
		return rc;
	}

	rc = createVariableInfo(db);
	if (rc != COEXIT_SUCCESS) {
		return rc;
	}

	rc = createCmdList(db);
	if (rc != COEXIT_SUCCESS) {
		return rc;
	}

	rc = createOperand(db);
	if (rc != COEXIT_SUCCESS) {
		return rc;
	}

	rc = createLinkType(db);
	if (rc != COEXIT_SUCCESS) {
		return rc;
	}

	return COEXIT_SUCCESS;
}

